//
// Created by Suny on 15/5/22.
// Copyright (c) 2015 Suny. All rights reserved.
//

#import "TopicView.h"
#import "UIImageView+WebCache.h"
@interface TopicView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *topicNameLabel;

@end
@implementation TopicView


- (void)setup 
{
	
	// positioning
	self.topMargin = 8;
	self.leftMargin = 8;
	
	// background
	self.backgroundColor = [UIColor colorWithHexString:@"FFFFFF"];
	
	// shadow
	self.layer.shadowColor = [UIColor colorWithHexString:@"c5c5c5"].CGColor;
	self.layer.shadowOffset = CGSizeMake(0, 0.5);
	self.layer.shadowRadius = 1;
	self.layer.shadowOpacity = 1;
}
- (UILabel *)titleLabel
{
	if (!_titleLabel)
 {
		_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		_titleLabel.backgroundColor = [UIColor clearColor];
		_titleLabel.textAlignment = NSTextAlignmentCenter;
		_titleLabel.font = [UIFont systemFontOfSize:18];
	}
	
	return _titleLabel;
}
- (UILabel *)topicNameLabel
{
	if (!_topicNameLabel)
	{
		_topicNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		_topicNameLabel.textAlignment = NSTextAlignmentCenter;
		_topicNameLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
		_topicNameLabel.font = [UIFont systemFontOfSize:18];

	}
	
	return _topicNameLabel;
}
- (UIImageView *)imageView
{
	if (!_imageView)
 {
		_imageView = [[UIImageView alloc] init];
	}
	
	return _imageView;
}

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{
		[self setup];
		[self addSubview:self.titleLabel];
		[self addSubview:self.imageView];
		[self addSubview:self.topicNameLabel];
	}
	
	return self;
}


+ (id)topicViewWithTopic:(TopicInfo *)topicInfo widthSize:(CGSize)size
{
	TopicView *topicView = [TopicView boxWithSize:size];
	topicView.topicInfo = topicInfo;
	
	
	return topicView;
}

- (void)layoutSubviews
{

	self.titleLabel.hidden = self.bigStyle;
	self.titleLabel.text = self.title;	
	
	self.titleLabel.frame = CGRectMake(5, 10, self.width - 2 * 5, 30);
	self.topicNameLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];

	if (self.bigStyle)
	{
		self.imageView.frame = CGRectMake(5, 5,
										  self.width - 10,
										  self.height - 10);
		
		self.topicNameLabel.frame = self.titleLabel.frame;

		self.topicNameLabel.bottom = self.imageView.bottom;
		
		self.topicNameLabel.textColor = [UIColor whiteColor];

	}
	else
	{
		self.imageView.frame = CGRectMake(5,
                                          self.titleLabel.bottom + 5,
                                          self.width - 10,
                                          self.height - 10 - self.titleLabel.bottom - 5 - 20);
		
		
		self.topicNameLabel.frame = self.titleLabel.frame;
		
		self.topicNameLabel.top = self.imageView.bottom;
		
		self.topicNameLabel.backgroundColor = [UIColor clearColor];
		self.topicNameLabel.textColor = [UIColor blackColor];


		
	}
	
	[self.imageView setQNImageWithURL:[NSURL URLWithString:self.topicInfo.thumbnail] 
					  placeholderImage:[UIImage imageNamed:@"default_01"]];
	
	self.topicNameLabel.text = self.topicInfo.name;


}

@end